﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading;
using System.Diagnostics.CodeAnalysis;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Xml.Linq;
using GalaSoft.MvvmLight;
using Microsoft.SilverlightMediaFramework.Utilities.Xml.Extensions;
using System.Windows.Media.Imaging;
using System.Xml;
using System.IO;
using YouTube.Model; 

namespace YouTube
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            YouTubeClient.Instance.getvideo(Callback);
        } 
        public Uri YouTubeUrl { get; set; }

        public string YouTubeID
        {
            get
            {
                return "";
            }
        }
        private VideoViewModel viewmodel;

        public void Callback()
        {
             //YouTubeUrl=    new Uri("http://www.youtube.com/watch?v=7Q7vLqemc0w&feature=feedrec_grec_index");
             //YouTubeUrl.OriginalString
             //        .Remove("&feature=youtube_gdata")
             //        .Remove("http://www.youtube.com/watch?v=");
            VideoModel model = null;
            try
            {
                //YouTubeClient.Instance.getViDeos("ShreyaGoshal");
                bool flag;
                model = YouTubeClient.Instance.GetVideoFromId(new VideoId("7Q7vLqemc0w"), this.PlayVideo, out flag);
                this.viewmodel.Model = model;
                if (flag)
                {
                    this.PlayVideo(null);
                }
            }
            catch (Exception e)
            {
                if (base.NavigationService.CanGoBack)
                {
                    base.NavigationService.GoBack();
                }
            }

        }
        private void PlayVideo(VideoModel playVideoError)
        {
            Dispatcher.BeginInvoke(() =>
            {
                if (playVideoError != null)
                {
                    Microsoft.Phone.Tasks.MediaPlayerLauncher pl = new Microsoft.Phone.Tasks.MediaPlayerLauncher();
                    pl.Media = playVideoError.MediaContentUriFormat2;
                  //  pl.Show();
                //   element.Source = playVideoError.MediaContentUriFormat3;
                }
            });
            //if (playVideoError != null)
            //{
            //    PersistentPlaybackState.Instance.CurrentVideo.LastKnownPlaybackState = PlaybackState.VideoFailedToPlay;
            //    this.ShowErrorMessage(ErrorString.ExceptionToYouTubeError(playVideoError));
            //}
            //else
            //{
            //    this.ProgressBar.Visibility = Visibility.Collapsed;
            //    this.ProgressBar.IsIndeterminate = false;
            //    VideoModel model = this.viewmodel.Model;
            //    if (((model != null) && (model.MediaContentUriFormat2 == null)) && ((model.MediaContentUriFormat3 == null) && (model.MediaContentUriFormat9 == null)))
            //    {
            //        PersistentPlaybackState.Instance.CurrentVideo.LastKnownPlaybackState = PlaybackState.VideoFailedToPlay;
            //        this.ShowErrorMessage(Strings.ErrorDialog_NoPvtAPIAvailable);
            //    }
            //    else
            //    {
            //        Uri uri = null;
            //        if ((NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) || (NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet))
            //        {
            //            uri = model.MediaContentUriFormat3 ?? (model.MediaContentUriFormat9 ?? model.MediaContentUriFormat2);
            //        }
            //        else
            //        {
            //            uri = model.MediaContentUriFormat2 ?? (model.MediaContentUriFormat9 ?? model.MediaContentUriFormat3);
            //        }
            //        if (string.IsNullOrEmpty(this.vndQueryArgs))
            //        {
            //            this.MediaPlayer.Source = uri;
            //        }
            //        else if (uri.OriginalString.Contains("?"))
            //        {
            //            this.MediaPlayer.Source = new Uri(uri.OriginalString + "&" + this.vndQueryArgs);
            //        }
            //        else
            //        {
            //            this.MediaPlayer.Source = new Uri(uri.OriginalString + "?" + this.vndQueryArgs);
            //        }
            //        switch (PersistentPlaybackState.Instance.CurrentVideo.LastKnownPlaybackState)
            //        {
            //            case PlaybackState.NeverPlayed:
            //                if (PersistentPlaybackState.Instance.AppLaunchMode != LaunchMode.LaunchedNonHubWithVideo)
            //                {
            //                    this.StartInPausedState(0L);
            //                    break;
            //                }
            //                this.StartWithActivePlayback(0L);
            //                break;

            //            case PlaybackState.PausedByUser:
            //            case PlaybackState.TrickModeToEnd:
            //            case PlaybackState.RewoundToBeginning:
            //            case PlaybackState.VideoObscured:
            //            case PlaybackState.ScreenLocked:
            //            case PlaybackState.Tombstoned:
            //                this.StartInPausedState(PersistentPlaybackState.Instance.CurrentVideo.LastKnownPosition);
            //                break;

            //            case PlaybackState.PlayedToEnd:
            //                if (PersistentPlaybackState.Instance.AppLaunchMode != LaunchMode.LaunchedNonHubWithVideo)
            //                {
            //                    this.StartInPausedState(0L);
            //                    break;
            //                }
            //                this.StartWithActivePlayback(0L);
            //                break;

            //            case PlaybackState.VideoFailedToPlay:
            //                this.StartInPausedState(PersistentPlaybackState.Instance.CurrentVideo.LastKnownPosition);
            //                break;

            //            case PlaybackState.PlaybackActive:
            //                if (PersistentPlaybackState.Instance.AppLaunchMode != LaunchMode.LaunchedNonHubWithVideo)
            //                {
            //                    this.StartInPausedState(PersistentPlaybackState.Instance.CurrentVideo.LastKnownPosition);
            //                    break;
            //                }
            //                this.StartWithActivePlayback(0L);
            //                break;
            //        }
            //        this.SendToHub();
            //    }
            //}
        }
        
    }
   
   
  
    
    

   




}